<template>
    <div class="container" v-if="readyShow">
        <el-steps :active="active" align-center v-if="agentTypeBoolean">
            <el-step title="债权人"></el-step>
            <el-step title="代理人"></el-step>
            <el-step title="债权信息"></el-step>
            <el-step title="资料上传"></el-step>
        </el-steps>
        <el-steps :active="active" align-center v-else="agentTypeBoolean">
            <el-step title="债权人"></el-step>
            <el-step title="债权信息"></el-step>
            <el-step title="资料上传"></el-step>
        </el-steps>
        <div style="margin-top: 8%"></div>
        <register-creditor v-show="page1" :form="form1" :creditorButton="creditorButton" @nextPage="nextPage1"
                           v-if="identityType == 1"></register-creditor>
        <register-organization v-show="page1" :form="form1" :creditorButton="creditorButton" @nextPage="nextPage1"
                               v-if="identityType == 2"></register-organization>
        <register-government v-show="page1" :form="form1" :creditorButton="creditorButton" @nextPage="nextPage1"
                             v-if="identityType == 3"></register-government>
        <register-agent v-show="page2" :form="form2" :agentButton="agentButton" @nextPage="nextPage2"
                        @previousPage="previousPage2" v-if="agentTypeBoolean"></register-agent>
        <register-debts v-show="page4" :form="form4" :showBack="showBack" @nextPage="nextPage4"
                        @previousPage="previousPage4"></register-debts>
        <register-file v-show="page5" :form="form5" @nextPage="nextPage5" @previousPage="previousPage5"></register-file>
        <el-dialog title="" :visible.sync="registerVisible" width="95%" center>
            <el-checkbox v-model="checked">认可通过工行短信、邮箱、公众号等送达方式</el-checkbox>
            <span slot="footer" class="dialog-footer">
                <el-button style="margin-right: 10%" type="primary" @click="registerVisible = false">取 消</el-button>
                <el-button style="margin-left: 10%" @click="toSubmit" type="primary" disabled v-if="!checked">提 交</el-button>
                <el-button style="margin-left: 10%" @click="toSubmit" type="primary" v-else="!checked">提 交</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import RegisterCreditor from 'components/register/register-creditor/register-creditor.vue'
    import RegisterOrganization from 'components/register/register-creditor/register-organization.vue'
    import RegisterGovernment from 'components/register/register-creditor/register-government.vue'
    import RegisterAgent from 'components/register/register-agent.vue'
    import RegisterAccount from 'components/register/register-account.vue'
    import RegisterDebts from 'components/register/register-debts.vue'
    import RegisterFile from 'components/register/register-file.vue'
    import util from 'assets/js/util.js'

    export default {
        data() {
            return {
                readyShow: false,
                registerVisible: false,
                showSubmit: false,
                checked: true,
                active: 0,
                page1: true,
                page2: false,
                page3: false,
                page4: false,
                page5: false,
                identityType: 1,
                natureBoolean: false,
                agentType: 0,
                especially:false,
                agentTypeBoolean: false,
                attendState: 0,
                showBack: true,
                form1: {
                    fileList: '',
                    identityType:'',
                    orgName: '',
                    orgCode: '',
                    name: '',
                    idType: 1,
                    //idTypeText: '身份证',
                    idcard: '',
                    mobile: '',
                    email: '',
                    address: '',
                    tel: '',
                    idcardFront: -1,
                    idcardReverse: -1,
                    bankUname: '',
                    bankName: '',
                    bankAccount: '',
                    specialCreditor:'',
                    specialNote:'',
                    especiallyFileList:'',
                },
                form2: {
                    name: '',
                    idcardFront: -1,
                    idcardReverse: -1,
                    idcardFrontVisible: false,
                    idcardReverseVisible: false,
                    idType: 1,
                    idcard: '',
                    mobile: '',
                    email: '',
                    address: '',
                    fileList: ''
                },
                form4: {
                    identityType: '1',
                    creditNature: '',
                    mainObligor: '',
                    relatedObligor:'',
                    type: '0',
                    giveFirst: '1',
                    //guaranteeHas: '0',
                    //guaranteeName: '',
                    guaranteeCond: '',
                    guaranteeForm: '0',
                    declarePrincipal: 0,
                    declareInterest: 0,
                    declareOther: 0,
                    declareTotal: 0,
                    declareProcess:'',
                    declareNote: ''
                },
                form5: {
                    fileList: ''
                },
                agentButton: '下一页',
                creditorButton: '下一页'
            }
        },
        mounted() {
            this.identityType = util.getUrlParam(document.location.href, 'identityType');
            this.agentType = util.getUrlParam(document.location.href, 'agentType');
            this.attendState = util.getUrlParam(document.location.href, 'attendState');
            this.natureBoolean = (this.identityType === '2');
            this.agentTypeBoolean = (this.agentType === '1');
            this.readyShow = true;
        },
        methods: {
            matchTypeText: function (type) {
                let typeText = '';
                switch (type) {
                    case 1:
                        typeText = '身份证';
                        break;
                    case 2:
                        typeText = '执业证';
                        break;
                    case 3:
                        typeText = '护照';
                        break;
                    default:
                        break
                }
                return typeText
            },
            toSubmit: function () {
                this.registerVisible = false;
                this.onSubmit();
            },
            nextPage1: function (formData) {
                this.form1 = formData;
                this.nextPage(true)
            },
            nextPage2: function (formData) {
                this.form2 = formData;
                this.nextPage(true)
            },
            nextPage4: function (formData) {
                this.form4 = formData;
                this.nextPage(true)
            },
            nextPage5: function (formData) {
                this.form5 = formData;
                this.registerVisible = true;
            },
            previousPage2: function (formData) {
                this.form2 = formData;
                this.nextPage(false)
            },
            previousPage4: function (formData) {
                this.form4 = formData;
                this.nextPage(false)
            },
            previousPage5: function (formData) {
                this.form5 = formData;
                this.nextPage(false)
            },

            nextPage: function (nextOrPrevious) {
                this.initialPage();
                if (nextOrPrevious) {
                    if (this.active++ > 3) this.active = 0
                }
                else {
                    if (this.active-- < 0) this.active = 0
                }
                if (this.agentTypeBoolean)
                    switch (this.active) {
                        case 0:
                            this.page1 = true;
                            break;
                        case 1:
                            this.page2 = true;
                            break;
                        case 2:
                            this.page4 = true;
                            break;
                        case 3:
                            this.page5 = true;
                            break
                    }
                else
                    switch (this.active) {
                        case 0:
                            this.page1 = true;
                            break;
                        case 1:
                            this.page4 = true;
                            break;
                        case 2:
                            this.page5 = true;
                            break
                    }
            },
            initialPage: function () {
                this.page1 = false;
                this.page2 = false;
                this.page4 = false;
                this.page5 = false;
            },
            onSubmit: function () {
                let form = new FormData();
                form.append('creditorInfo.creditId', util.getUrlParam(document.location.href, 'creditId'));
                form.append('creditorInfo.caseId', util.getUrlParam(document.location.href, 'caseId'));
                form.append('creditorInfo.identityType', this.identityType);
                form.append('creditorInfo.agentType', this.agentType);
                form.append('creditorInfo.attendState', this.attendState);
                if (this.form1.idcardFront !== -1)
                    form.append('creditorInfo.idcardFront', this.form1.idcardFront);
                if (this.form1.idcardReverse !== -1)
                    form.append('creditorInfo.idcardReverse', this.form1.idcardReverse);
                if (this.form1.name !== '')
                    form.append('creditorInfo.name', this.form1.name);
                if (this.identityType !== 3)
                    form.append('creditorInfo.idType', this.form1.idType);
                if (this.form1.idcard !== '')
                    form.append('creditorInfo.idcard', this.form1.idcard);
                if (this.form1.mobile !== '')
                    form.append('creditorInfo.mobile', this.form1.mobile);
                if (this.form1.specialCreditor !== '')
                    form.append('creditorInfo.specialCreditor', this.form1.specialCreditor);
                if (this.form1.specialNote !== '')
                    form.append('creditorInfo.specialNote', this.form1.specialNote);

                if (this.form1.specialFileIds !== '')
                    form.append('creditorInfo.especiallyFileList', this.form1.especiallyFileList);
                if (this.form1.tel !== '')
                    form.append('creditorInfo.tel', this.form1.tel);
                if (this.form1.email !== '')
                    form.append('creditorInfo.email', this.form1.email);
                form.append('creditorInfo.address', this.form1.address);
                if (this.form1.orgFileIds !== '')
                    form.append('creditorInfo.fileList', this.form1.fileList);
                if (this.form1.orgName !== '')
                    form.append('creditorInfo.orgName', this.form1.orgName);
                if (this.form1.orgCode !== '')
                    form.append('creditorInfo.orgCode', this.form1.orgCode);
                if (this.agentTypeBoolean) {
                    if (this.form2.idcardFrontVisible)
                        form.append('creditorInfo.idcardReverse', this.form2.idcardReverse);
                    if (this.form2.idcardReverseVisible)
                        form.append('creditorInfo.idcardFront', this.form2.idcardFront);
                    form.append('creditorInfo.name', this.form2.name);
                    form.append('creditorInfo.address', this.form2.address);
                    form.append('creditorInfo.idType', this.form2.idType);
                    form.append('creditorInfo.idcard', this.form2.idcard);
                    form.append('creditorInfo.mobile', this.form2.mobile);
                    if (this.form2.email !== '')
                        form.append('creditorInfo.email', this.form2.email);
                    form.append('creditorInfo.agentDetailFileids', this.form2.agentDetailFileids)
                }
                form.append('creditorInfo.bankName', this.form1.bankName);
                form.append('creditorInfo.bankUname', this.form1.bankUname);
                form.append('creditorInfo.bankAccount', this.form1.bankAccount);

                form.append('creditInfo.identityType', this.form4.identityType);
                if (this.form4.mainObligor !== '')
                    form.append('creditInfo.mainObligor', this.form4.mainObligor);
                if (this.form4.type !== 0)
                    form.append('creditInfo.type', this.form4.type);
                if (this.form4.type === 4) {
                    form.append('creditInfo.giveFirst', this.form4.giveFirst);
                    if (this.form4.guaranteeCond !== '')
                        form.append('creditInfo.guaranteeCond', this.form4.guaranteeCond);
                    if (this.form4.guaranteeForm !== '1')
                        form.append('creditInfo.guaranteeForm', this.form4.guaranteeForm)
                }
                form.append('creditInfo.declarePrincipal', this.form4.declarePrincipal);
                form.append('creditInfo.declareInterest', this.form4.declareInterest);
                form.append('creditInfo.declareOther', this.form4.declareOther);
                form.append('creditInfo.declareProcess', this.form4.declareProcess);

                if (this.form4.declareNote !== '')
                    form.append('creditInfo.declareNote', this.form4.declareNote);

                form.append('creditInfo.declaerFileids', this.form5.declaerFileids);

                util.ajaxByForm({
                    url: '/caseInfo/addCreditorAllInfo',
                    form: form,
                    success: function (data) {
                        if (data.message === true)
                            window.location.href = '../index/index.html?';
                        else
                            alert(data.error);
                    }
                });
            }
        },
        components: {
            RegisterCreditor,
            RegisterOrganization,
            RegisterGovernment,
            RegisterAgent,
            RegisterAccount,
            RegisterDebts,
            RegisterFile
        }
    };
</script>

<style>
    .container {
        margin-top: 5%;
    }

    .el-step__main div {
        font-size: 10px;
    }
</style>
